<template>
	<view v-if="pageShow">
		<view class="headimg flexcenter" @click="uploadImg(1,avatar,avatar_text)">
			<image :src="avatar_text" mode=""></image>
			<view class="xiangji"></view>
		</view>
		<view class="ffffff mainpadding_top flexbetween" style="margin-top: 60rpx;">
			<view class="titletext hei_text bold">昵称</view>
			<input type="text" placeholder="请输入" style="text-align: right;" class="hei_text main_size bold"
				v-model="nickname">
		</view>
		<view class="ffffff mainpadding_top flexbetween" style="margin-top: 60rpx;">
			<view class="titletext hei_text bold">邮箱</view>
			<input type="text" placeholder="请输入" style="text-align: right;" class="hei_text main_size bold"
				v-model="email">
		</view>
		<uni-data-picker @change="bindPickerChange" :localdata="items" placeholder="选择地区" popup-title="选择地区"
			v-model="area_id">
			<view class="ffffff mainpadding_top flexbetween" style="margin-top: 60rpx;">
				<view class="titletext hei_text bold">城市</view>
				<input type="text" placeholder="点击选择" style="text-align: right;" class="hei_text main_size bold"
					v-model="city_text">
			</view>
		</uni-data-picker>

		<view class="ffffff mainpadding_top flexbetween" style="margin-top: 60rpx;">
			<view class="titletext hei_text bold">手机号码</view>
			<view class="hei_text main_size bold">{{mobile}}</view>
		</view>
		<!-- <view class="headimg flexcenter">
			<image src="../../static/logo.png" mode=""></image>
		</view>
		<view class="ffffff mainpadding_top flexbetween" style="margin-top: 60rpx;">
			<view class="titletext hei_text bold">昵称</view>
			<input type="text" placeholder="请输入" style="text-align: right;" class="hei_text main_size bold" v-model="nickname">
		</view> -->
		<view class="posibottom mainpadding" style="bottom: 0;">
			<view class="submitbtn" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	import citys from "@/static/city/city.js"
	export default {
		data() {
			return {
				items: citys.data,
				city_text: "",
				province_id: "",
				city_id: "",
				area_id: "",
				avatar: "",
				avatar_text: "",
				nickname: "",
				pageShow: true,
				email:"",
				mobile:"",
			}
		},
		onShow() {
			this.init()
		},
		onShareAppMessage: function(options) {
			return {
				title: '制片宝',
				imageUrl: '',
				path: '/pages/index/index'
			}
		},
		onShareTimeline: function() {
			return {
				title: '制片宝',
				imageUrl: '',
				path: '/pages/index/index'
			}
		},
		methods: {
			bindPickerChange(val) {
				let area = val.detail.value
				let area_text = [area[0].text, area[1].text, area[2].text];
				this.city_text = area_text.join("/")
				this.province_id = area[0].value;
				this.city_id = area[1].value;
				this.area_id = area[2].value;
			},
			submit() {
				httpRequest.request('user/profile', 'GET', {
					nickname: this.nickname,
					avatar: this.avatar,
					email:this.email,
					province_id:this.province_id,
					city_id:this.city_id,
					area_id:this.area_id,
				}, false, false, true).then(res => {
					if (res.code == 1) {
						httpRequest.toast("修改成功");
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}else{
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			init() {
				httpRequest.request('user/index', 'GET', {}, false, false, true).then(res => {
					this.avatar = res.data.avatar
					this.avatar_text = res.data.avatar_text
					this.nickname = res.data.nickname
					this.mobile = res.data.mobile
					this.email = res.data.email
					this.city_text = res.data.cityinfo=="//"?"":res.data.cityinfo
					this.province_id = res.data.province_id
					this.city_id = res.data.city_id
					this.area_id = res.data.area_id
					this.pageShow = true
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			uploadImg(value, upImgs, upImgs_text) {
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.avatar = res.url;
					this.avatar_text = image_item
				}).catch(err => {});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.headimg {
		width: 180rpx;
		height: 180rpx;
		margin: 30rpx auto 0;
		padding-top: 30rpx;
		position: relative;

		image {
			width: 101%;
			height: 100%;
			border-radius: 50%;
		}

		.xiangji {
			width: 40rpx;
			height: 40rpx;
			position: absolute;
			bottom: 0;
			right: 0;
			background-size: 100% 100%;
			background-image: url('@/static/image/system/avatarup.png');
		}
	}
</style>